package com.hgh.nettydemo.netty.nettyclient.config;

import com.hgh.nettydemo.netty.nettyclient.protocolInitalizer.ClientMessageProtocolInitalizer;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @author: huguanghui
 * Created by huguanghui on 2018/6/18.
 * @Description:
 */
@Configuration
public class NettyClientConfig {


    @Autowired
    private ClientMessageProtocolInitalizer clientMessageProtocolInitalizer;


    @SuppressWarnings("unchecked")
    @Bean(name = "bootstrap")
    public Bootstrap bootstrap() {
        EventLoopGroup nioEventLoopGroup = new NioEventLoopGroup(2);
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(nioEventLoopGroup)
                .channel(NioSocketChannel.class)
                .handler(clientMessageProtocolInitalizer)
                .option(ChannelOption.SO_KEEPALIVE, true)
                .option(ChannelOption.SO_BACKLOG, 100)
                .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);

        return bootstrap;
    }
}
